package com.bc.web.config;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import com.bc.web.config.route.AdminRoute;
import com.bc.web.config.route.MerchRoute;
import com.bc.web.config.route.OrgRoute;
import com.bc.web.controller.CustomerController;
import com.bc.web.controller.IndexMyController;
import com.bc.web.controller.Qqueryexchagerate;
import com.bc.web.interceptors.AuthInterceptor;
import com.bc.web.interceptors.ExceptionInterceptor;
import com.bc.web.interceptors.SessionTimeOutInterceptor;
import com.bc.web.service.ClearD0Task;
import com.fs.common.auth.FSAuth;
import com.fs.common.util.FSThreadPoolExecutor;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.json.FastJsonFactory;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
//import com.jfplugin.mail.MailPlugin;

public class SystemConfig extends JFinalConfig{
	
	public static final Prop config = PropKit.use("config.properties");

	@Override
	public void configConstant(Constants me) {
		me.setDevMode(config.getBoolean("dev"));
		me.setViewType(ViewType.JFINAL_TEMPLATE);
		me.setJsonFactory(new FastJsonFactory());
		me.setError404View("/WEB-INF/error/404.html");
	}

	@Override
	public void configEngine(Engine me) {
		me.setDevMode(config.getBoolean("dev"));
		me.addDirective("auth", FSAuth.class);
	}
	
	@Override
	public void afterJFinalStart() {
		super.afterJFinalStart();
		int corePoolSie = SystemConfig.config.getInt("thread_corePoolSize");
		int maximumPoolSize = SystemConfig.config.getInt("thread_maximumPoolSize");
		long keepAliveTime = SystemConfig.config.getLong("thread_keepAliveTime");
		FSThreadPoolExecutor.init(corePoolSie, maximumPoolSize, keepAliveTime, TimeUnit.HOURS, new LinkedBlockingQueue<>());
		System.out.println("------------------- 初始化线程池配置成功 -------------------");
		
	}

	@Override
	public void configHandler(Handlers arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
		me.add(new SessionTimeOutInterceptor());
		me.add(new AuthInterceptor());
		me.add(new ExceptionInterceptor());
	}

	@Override
	public void configPlugin(Plugins me) {
		final Prop db = PropKit.use("db.properties");
		boolean showSql = db.getBoolean("showSql");
		String url = db.get("jdbcUrl");
		String userName = db.get("userName");
		String password = db.get("password");
		int initialSize = db.getInt("initialSize");
		int minIdle = db.getInt("minIdle");
		int maxActive = db.getInt("maxActive");
		//后台数据源
		DruidPlugin mainDB = new DruidPlugin(url,userName,password);
		mainDB.set(initialSize, minIdle, maxActive);
		me.add(mainDB);
		ActiveRecordPlugin arp = new ActiveRecordPlugin(mainDB);
		arp.setShowSql(showSql);
		MappingKit.mapping(arp);
		me.add(arp);
		
		String tradeUrl = db.get("trade_jdbcUrl");
		String tradeUserName = db.get("trade_userName");
		String tradePassword = db.get("trade_password");
		int tradeInitialSize = db.getInt("trade_initialSize");
		int tradeMinIdle = db.getInt("trade_minIdle");
		int tradeMaxActive = db.getInt("trade_maxActive");
		//后台数据源
		DruidPlugin tradeDB = new DruidPlugin(tradeUrl,tradeUserName,tradePassword);
		tradeDB.set(tradeInitialSize, tradeMinIdle, tradeMaxActive);
		me.add(tradeDB);
		ActiveRecordPlugin tradeArp = new ActiveRecordPlugin(R.BD_TRADE, tradeDB);
		tradeArp.setShowSql(showSql);
		MappingKit.mappingTrade(tradeArp);
		me.add(tradeArp);
		
		//me.add(new MailPlugin(PropKit.use("mail.properties").getProperties()));
		
		boolean isRunTask = config.getBoolean("isRunTask");
		if(isRunTask) {
			Cron4jPlugin cp = new Cron4jPlugin();
			//cp.addTask("00 17 * * *", new ClearD0Task());
			cp.addTask("00 00 * * *", new ClearD0Task());
			me.add(cp);
			System.out.println("------------------- 定时任务配置成功-------------------");
		}
		
	}

	@Override
	public void configRoute(Routes me) {
		me.add(new AdminRoute());
		me.add(new OrgRoute());
		me.add(new MerchRoute());
		
		me.setBaseViewPath("/WEB-INF/");
		me.add("/rate", Qqueryexchagerate.class, "/");
		me.add("/customer", CustomerController.class, "/");
		me.add("/", IndexMyController.class, "/");
	}

}
